/**
 * Copyright (c) 2007-2008 Nearchos Paspallis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * AspectSense
 *
 * Middleware for annotation-based, context-aware and adaptive services for
 * mobile and ubiquitous computing
 *
 * This project is an open-source effort, initiated by Nearchos Paspallis as
 * part of his PhD work at the University of Cyprus (http://www.ucy.ac.cy).
 *
 * More information about the project is available at "http://aspectsense.org".
 */

package org.aspectsense.gui.client.tree;

import org.aspectsense.framework.MonitoringManagement;
import org.aspectsense.framework.RequiredServiceMetadata;
import org.aspectsense.metadata.ComponentMetadata;
import org.aspectsense.util.Miscellaneous;

import javax.swing.tree.TreeNode;
import java.rmi.RemoteException;
import java.util.Map;
import java.util.Set;

/**
 * @author Nearchos Paspallis [nearchos@cs.ucy.ac.cy]
 *         Date: Jan 8, 2008
 *         Time: 11:43:54 AM
 */
public class ComponentsTreeNode extends DefaultTreeNode
{
    public static final String INSTALLED_COMPONENTS_ROOT = "Installed components view";

    public ComponentsTreeNode(final String [] installedComponentIDs, final MonitoringManagement monitoringManagement)
            throws RemoteException
    {
        super(null);

        for(String installedComponentID : installedComponentIDs)
        {
            final ComponentMetadata componentMetadata
                    = monitoringManagement.getComponentMetadata(installedComponentID);
            final String providedServiceID
                    = componentMetadata.getProvidedService().getServiceTypeCanonicalName();

            final double providedUtility
                    = monitoringManagement.getServiceUtility(providedServiceID, installedComponentID);

            children.add(new ComponentMetadataTreeNode(this, componentMetadata, providedUtility));
        }
    }

    public String toString()
    {
        return INSTALLED_COMPONENTS_ROOT;
    }

    class ComponentMetadataTreeNode extends DefaultTreeNode
    {
        private final ComponentMetadata componentMetadata;
        private final String name;

        ComponentMetadataTreeNode(final TreeNode parent, final ComponentMetadata componentMetadata, final double utility)
        {
            super(parent);

            this.componentMetadata = componentMetadata;
            this.name = componentMetadata.getDisplayID();

            children.add(new DefaultProvidedServiceRootTreeNode(this, componentMetadata, utility));

            final Map<String,RequiredServiceMetadata> requiredServiceMetadataMap
                    = componentMetadata.getRequiredServices();
            if(!requiredServiceMetadataMap.isEmpty())
            {
                children.add(new DefaultRequiredServicesTreeNode(this, componentMetadata));
            }
        }

        public String toString()
        {
            return name;
        }

        public String getTooltip()
        {
            final String name = componentMetadata.getDisplayID();

            return "<html>Name: <i>" + name + "</i>" +
                    "<br></br>Type: <i>" + componentMetadata.getComponentTypeCanonicalName() + "</i>" +
                    "<br></br>Is-stateful: <i>" + componentMetadata.isStateful() + "</i>" +
                    "</html>";
        }
    }

    class DefaultProvidedServiceRootTreeNode extends DefaultTreeNode
    {
        private final String message = "Provided service";

        DefaultProvidedServiceRootTreeNode(final TreeNode parent, final ComponentMetadata componentMetadata, final double utility)
        {
            super(parent);

            children.add(new DefaultProvidedServiceTreeNode(this, componentMetadata, utility));
        }

        public String toString()
        {
            return message;
        }
    }

    class DefaultProvidedServiceTreeNode extends DefaultTreeNode
    {
        private final String name;
        private final String serviceType;
        private final String description;
        private final String utilityS;

        DefaultProvidedServiceTreeNode(final TreeNode parent, final ComponentMetadata componentMetadata, final double utility)
        {
            super(parent);

            this.serviceType = componentMetadata.getProvidedService().getServiceTypeCanonicalName();
            final String serviceTypeCanonicalName = componentMetadata.getProvidedService().getServiceTypeCanonicalName();
            this.name = Miscellaneous.partAfterLastDot(serviceTypeCanonicalName);
            this.description = componentMetadata.getProvidedService().getDescription();
            this.utilityS = Miscellaneous.formatDoubleWithTwoDigitsMax(utility);
        }

        public String toString()
        {
            return name + " (utility: " + utilityS + ")";
        }

        public String getTooltip()
        {
            return "<html>Name: <i>" + name + "</i>" +
                    "<br></br>Type: <i>" + serviceType + "</i>" +
                    "<br></br>Description: <i>" + description + "</i>" +
                    "<br></br>Utility: <i>" + utilityS + "</i>" +
                    "</html>";
        }

        public boolean isLeaf()
        {
            return true;
        }

        public boolean getAllowsChildren()
        {
            return false;
        }
    }

    class DefaultRequiredServicesTreeNode extends DefaultTreeNode
    {
        private final String message = "Required services";

        DefaultRequiredServicesTreeNode(final TreeNode parent, final ComponentMetadata componentMetadata)
        {
            super(parent);

            final Map<String,RequiredServiceMetadata> requiredServiceMetadataMap
                    = componentMetadata.getRequiredServices();

            final Set<String> requiredServiceIDs
                    = componentMetadata.getRequiredServices().keySet();

            for(final String requiredServiceID : requiredServiceIDs)
            {
                final RequiredServiceMetadata requiredServiceMetadata
                        = requiredServiceMetadataMap.get(requiredServiceID);

                children.add(new DefaultRequiredServiceTreeNode(this, requiredServiceMetadata));
            }
        }

        public String toString()
        {
            return message;
        }
    }

    class DefaultRequiredServiceTreeNode extends DefaultTreeNode
    {
        private final String serviceName;
        private final String description;
        private final String serviceType;
        private final boolean mustBeLocal;

        DefaultRequiredServiceTreeNode(final TreeNode parent, final RequiredServiceMetadata requiredServiceMetadata)
        {
            super(parent);

            this.serviceType = requiredServiceMetadata.getServiceTypeCanonicalName();
            this.serviceName = Miscellaneous.partAfterLastDot(serviceType);
            this.description = requiredServiceMetadata.getDescription();
            this.mustBeLocal = requiredServiceMetadata.mustBeLocal();
        }

        public String toString()
        {
            return serviceName;
        }

        public String getTooltip()
        {
            return "<html>Name: <i>" + serviceName + "</i>" +
                    "<br></br>Type: <i>" + serviceType + "</i>" +
                    "<br></br>Description: <i>" + description + "</i>" +
                    "<br></br>Must-be-local: <i>" + mustBeLocal + "</i>" +
                    "</html>";
        }

        public boolean isLeaf()
        {
            return true;
        }

        public boolean getAllowsChildren()
        {
            return false;
        }
    }
}